package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Product;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IProductService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PageResult;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("product")
public class ProductController {

    @Autowired
    private IProductService productService;

    @RequestMapping("view")
    @RequiresPermissions(value = {"product:view","产品信息页面"},logical = Logical.OR)
    public String view() throws Exception{

        return "product/product";
    }

    @RequestMapping("selectAll")
    @ResponseBody
    public List<Product> selectAll() throws Exception{
        List<Product> products = productService.selectAll();
        Product p = new Product();
        p.setId(-1L);
        p.setName("---所有---");
        products.add(p);
        System.out.println(154987);
        return products;
    }
    @RequestMapping("selectNomal")
    @ResponseBody
    public List<Product> selectNomal() throws Exception{
        List<Product> products = productService.selectNomal();
        return products;
    }
    @RequestMapping("saveOrUpdate")
    @ResponseBody
    @RequiresPermissions(value = {"product:saveOrUpdate","产品信息新增/修改"},logical = Logical.OR)
    public JsonResult saveOrUpdate(Product entity,String promisename,Long ids){
        JsonResult jsonResult = new JsonResult();
        try {
            if (entity.getId()!=null){
                productService.updateByPrimaryKey(entity,promisename,ids);
            }else {
                productService.insert(entity);
            }
        }catch (Exception e) {
            jsonResult.mark("操作失败");
        }
        return jsonResult;
    }

    @RequestMapping("status")
    @ResponseBody
    @RequiresPermissions(value = {"product:status","产品信息销售状态修改"},logical = Logical.OR)
    public JsonResult status(Long id) throws Exception{
        JsonResult jsonResult = new JsonResult();
        try{
            productService.changeState(id);
        }catch (Exception e){
            jsonResult.mark("操作失败");
        }
        return jsonResult;
    }

    //tao
    @RequestMapping("selectByPrimaryKey")
    @ResponseBody
    public Object selectByPrimaryKey(Long id) throws Exception{
        List<Product> list =new ArrayList<>();
        Product product = productService.selectByPrimaryKey(id);
        list.add(product);
        return list;
    }


    @RequestMapping("list")
    @ResponseBody
    public PageResult list(QueryObject qo) throws Exception{
        return productService.query(qo);
    }




}
